//
//  NSString+extension.m
//  Usan
//
//  Created by gongxiang on 12/5/18.
//  Copyright © 2018 gongxiang. All rights reserved.
//

#import "NSString+extension.h"

@implementation NSString (extension)
+(NSString *)appVersion{
    return [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
}
-(NSString *)replaceWithStartType{
    if (!self.length) {
        return @"";
    }
    NSMutableString *str = [[NSMutableString alloc] init];
    for (NSInteger i = 0; i < self.length; i++) {
        unichar c = [self characterAtIndex:i];
        if (i > 2 && i < self.length - 4) {
            [str appendString:@"*"];
        }else{
            [str appendString:[NSString stringWithFormat:@"%C",c]];
        }
    }
    return str;
}

+(NSMutableAttributedString *)attrStrFrom:(NSString *)allString colorStr:(NSString *)colorStr color:(UIColor *)color font:(CGFloat )font
{
    NSMutableAttributedString *arrString = [[NSMutableAttributedString alloc]initWithString:allString];
    // 设置前面几个字串的格式:字号字体、颜色
    [arrString addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:font],
                               NSForegroundColorAttributeName:color
                               }
                       range:[allString rangeOfString:colorStr]];
    return arrString;
}

+(NSString *)BaseSystem16ToBaseSystem10:(NSString *)str{
    return [NSString stringWithFormat:@"%lu",strtoul(str.UTF8String, 0, 16)];
}
+(NSString *)BaseSystem10ToBaseSystem16:(NSString *)str{
    
    return [NSString stringWithFormat:@"%llx",str.longLongValue];
    
    
//    NSLog(@"16进制转10进制 = %lu",strtoul(@"0b".UTF8String, 0, 16));
//    NSLog(@"16进制转10进制 = %lu",strtoul(@"0104020000b930".UTF8String, 0, 10));
//    NSLog(@"10进制转16进制 = %@",[NSString stringWithFormat:@"%x",@"0104020000b930".longLongValue]);//unsigned
//    NSLog(@"%@",[self getIPAddresses]);
}

+ (NSMutableArray *)toBinarySystemWithDecimalSystem:(NSInteger)num length:(int)length{
    int remainder = 0;      //余数
    int divisor = 0;        //除数
    NSString * prepare = @"";
    while (true)
    {
        remainder = num%2;
        divisor = num/2;
        num = divisor;
        prepare = [prepare stringByAppendingFormat:@"%d",remainder];
        if (divisor == 0)
        {
            break;
        }
    }
    //倒序输出
    NSString * result = @"";
    for (int i = length -1; i >= 0; i --)
    {
        if (i <= prepare.length - 1) {
            result = [result stringByAppendingFormat:@"%@",
                      [prepare substringWithRange:NSMakeRange(i , 1)]];
        }else{
            result = [result stringByAppendingString:@"0"];
        }
    }
    
    NSMutableArray *characters = [[NSMutableArray alloc] initWithCapacity:[result length]];
    for (int i = (int)[result length] - 1; i >= 0; i--) {
        NSString *ichar  = [NSString stringWithFormat:@"%c", [result characterAtIndex:i]];
        [characters addObject:ichar];
    }
//    self.logArr = (NSMutableArray *)[[self.logArr reverseObjectEnumerator] allObjects];
//    NSString *str = [characters componentsJoinedByString:@""];
//    NSArray *arr = [str componentsSeparatedByString:@""];
//    NSLog(@"%@",characters);
    return characters;
}

//时间格式的字符串转date
+(NSDate *)strTurnDate:(NSString *)time format:(NSString *)format{
//    NSString *birthdayStr=@"1986-03-28 00:00:00.000";
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:format];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:8]];//解决8小时时间差问题
    return [dateFormatter dateFromString:time];
}

//date转字符串
+(NSString *)dateTurnStr:(NSDate *)date format:(NSString *)format{
//    NSDate *date = [NSDate date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:format];
    return [dateFormatter stringFromDate:date];
}

- (NSData*)ad_hexStringToData {
    if (self.length % 2 != 0) {
        return nil;
    }
    NSMutableData *mulData = [[NSMutableData alloc] init];
    for (NSInteger i = 0; i < self.length; i += 2) {
        NSString *hexChar = [self substringWithRange:NSMakeRange(i, 2)];
        NSScanner *scanner = [[NSScanner alloc] initWithString:hexChar];
        unsigned int anInt;
        [scanner scanHexInt:&anInt];
        NSData *data = [[NSData alloc] initWithBytes:&anInt length:1];
        [mulData appendData:data];
    }
    return mulData;
}
@end
